package com.gupao.springbootmybatis.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.gupao.springbootmybatis.domain.User;
import com.gupao.springbootmybatis.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "", tags = "用户接口")
@ApiSort(value = 1)
@RestController
@RequestMapping(value="/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/listone")
    @ApiOperation(value = "listOne接口", notes = "根据用户名查询用户")
    public User listOne(String username){
        return userService.findByUsername(username);
    }

    @ApiOperation(value = "userList接口", notes = "查询用户列表")
    @GetMapping("/listall")
    public List<User> listAll(){
        return userService.listUser();
    }

    @ApiOperation(value = "add接口", notes = "新增用户")
    @PostMapping(value="/add")
    public String add(User user){
        int result=userService.insertUser(user);
        if(result>=1){
            return "添加成功";
        }else{
            return "添加失败";
        }
    }

    @ApiOperation(value = "update接口", notes = "修改用户")
    @PostMapping(value="/update")
    public String update(User user){
        int result=userService.updateUser(user);
        if(result>=1){
            return "修改成功";
        }else{
            return "修改失败";
        }
    }

    @ApiOperation(value = "delete接口", notes = "删除用户")
    @GetMapping(value="/delete")
    public String delete(int id){
        int result=userService.delete(id);
        if(result>=1){
            return "删除成功";
        }else{
            return "删除失败";
        }
    }
}